package com.zhoug.player.core.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import androidx.annotation.NonNull;

/**
 * @Author: zhoug
 * @Date: 2023/1/11
 * @Description:
 */
public class Utils {

    /**
     * 获取文件的后缀
     *
     * @param path
     * @return
     */
    public static String getSuffix(String path) {
        if (path == null) return null;
        int index = path.lastIndexOf(".");
        if (index >= 0 && index < path.length() - 1) {
            return path.substring(index + 1);
        } else {
            return null;//无后缀
        }

    }

    /**
     * 保存Bitmap到文件
     *
     * @param bitmap  Bitmap
     * @param quality 质量
     * @param path    保存路径
     * @return
     */
    public static boolean keepBitmap(Bitmap bitmap, int quality, String path) {
        String suffix = getSuffix(path);
        Bitmap.CompressFormat format = Bitmap.CompressFormat.PNG;
        if (suffix != null) {
            if (suffix.equalsIgnoreCase("jpeg") || suffix.equalsIgnoreCase("jpg")) {
                format = Bitmap.CompressFormat.JPEG;
            }
        }

        FileOutputStream fos = null;
        boolean compress = false;
        try {
            fos = new FileOutputStream(new File(path));
            compress = bitmap.compress(format, quality, fos);
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return compress;
    }

    /**
     * 拍摄照片和视频后相册中没有文件，的解决办法
     *
     * @param context
     * @param path
     */
    public static void scannerFile(Context context, String path) {
        try {
            if (path != null && new File(path).exists()) {
                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                Uri uri = Uri.fromFile(new File(path));
                intent.setData(uri);
                context.sendBroadcast(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static String formatTime(long milliseconds) {
        int time = (int) (milliseconds / 1000);
        int seconds = time % 60;
        int minute = time / 60;
        int hour = minute / 60;
        if (hour > 0) {
            minute = minute % 60;
            return addZero(hour) + ":" + addZero(minute) + ":" + addZero(seconds);
        } else {
            return addZero(minute) + ":" + addZero(seconds);
        }
    }


    public static String addZero(int num) {
        String s = String.valueOf(num);
        if (s.length() == 1) {
            return "0" + s;
        } else {
            return s;
        }
    }

    /**
     * 重命名文件
     *
     * @param src  源文件
     * @param dest 新文件
     * @return true:成功,false:失败
     */
    public static boolean renameFile(@NonNull File src, @NonNull File dest) {
        try {
            return src.renameTo(dest);
        } catch (Exception e) {
            LogUtils.e(">>>", "renameFile:" + e.getMessage());
        }
        return false;
    }

    /**
     * 复制文件
     *
     * @param src  源文件
     * @param dest 目标文件
     * @return true:成功,false:失败
     */
    public static boolean copyFile(@NonNull File src, @NonNull File dest, boolean deleteSrc) {
        boolean success;
        try (InputStream in = new FileInputStream(src);
             OutputStream out = new FileOutputStream(dest)) {
            byte[] buf = new byte[1024 * 8]; // 8KB缓冲区
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.flush();
            if (deleteSrc) {
                boolean delete = src.delete();// 删除原文件
            }
            success = true;
        } catch (IOException e) {
            LogUtils.e(">>>", "renameFile:" + e.getMessage());
            success = false;
        }
        return success;
    }


    /**
     * 移动文件
     *
     * @param src  源文件
     * @param dest 新文件
     * @return
     */
    public static boolean remoteFile(@NonNull File src, @NonNull File dest) {
        //同分区直接重命名
        boolean success = renameFile(src, dest);
        if (!success) {
            //跨分区复制,并删除源文件
            success = copyFile(src, dest, true);
        }
        return success;
    }

}
